package com.traveldream.backend.managed_beans;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.traveldream.entities.Flight;
import com.traveldream.entities.Place;
import com.traveldream.entities.Product;
import com.traveldream.entity_managers.ProductManager;


@ManagedBean(name="flightBackend")
@SessionScoped
public class FlightBean {

	public static final String WEB_PATH = "/backend/product/flight/";
	
	// instance of product manager
	@EJB
	private ProductManager pm;
	
	private Flight current; // current flight
	private List list = null;  // flight list
	private String filter;
	
	public FlightBean(){
	}
	
    // list action
	public String listAction(){
		this.list = pm.findAllFlights();
		return WEB_PATH + "list?";
	}
	
	// create action
	public String createAction(){
		current = new Flight();
		current.setProduct(new Product());
		current.setPlace(new Place());
		current.getProduct().setPlace(new Place());
		
		current.setDepartureDate(new Date());
		current.setArrivalDate(new Date());
		return WEB_PATH + "new" + "";
	}
	
	// edit action
	public String editAction(String id){

		int flightId = Integer.parseInt(id);
		
		current = pm.findFlight(flightId);
		return WEB_PATH + "edit";
	}
	
	// delete action
	public String deleteAction(String id){
		int flightId = Integer.parseInt(id);
		if(pm.delete(flightId)) 
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Eliminazione escursione avvenuta correttamente"));
			this.list = null; // to force refresh
		}
		else	
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Eliminazione escursione fallita!"));
		return WEB_PATH + "list";
	}
	
	// create new flight
	public String create()
	{
		System.out.print("create method");
        FacesMessage doneMessage = null;
		
		// where to go after creation
        String outcome = null;
        
        if (pm.create(this.current)!=null) {
            doneMessage = new FacesMessage("Volo aggiunto correttamente");
            FacesContext.getCurrentInstance().addMessage(null, doneMessage);
            outcome = "list?faces-redirect=true";
            this.list = null; //to force refresh
        } else {
            doneMessage = new FacesMessage("Aggiunta volo fallita!");
            outcome = "new";
        }
        return outcome;
	}
	
	
	public String update()
	{
		
		System.out.print("update method"+this.current.getProductId());
		FacesMessage doneMessage = null;
		
		// where to go after creation
        String outcome = null;
        if (pm.update(this.current)!=null) {
            doneMessage = new FacesMessage("Volo modificato correttamente");
            outcome = "list?faces-redirect=true";
            this.list = null; //to force refresh
        } else {
            doneMessage = new FacesMessage("Modifica volo fallita!");
            outcome = "edit";
        }
        FacesContext.getCurrentInstance().addMessage(null, doneMessage);
        return outcome;
	}
	
	// getters & setters
	public Flight getCurrent()
	{
		return this.current;
	}
	
	public void setCurrent(Flight current){
		this.current = current;
	}
	
	public List getList() {
		if(this.list == null)
		{
			this.list = pm.findAllFlights();
		}
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	

	public String getFilter() {
		return filter;
	}


	public void setFilter(String filter) {
		this.filter = filter;
	}
}